package view.controller;

import com.alibaba.fastjson.JSON;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.embed.swing.SwingFXUtils;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.*;
import javafx.stage.Stage;
import utils.*;

import javax.imageio.ImageIO;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.ResourceBundle;
/**
 * @author
 */
public class RegisterViewController implements Initializable {
    @FXML
    private ImageView imageview;

    @FXML
    private TextField url;

    @FXML
    private TextField area;

    @FXML
    private TextField phoneNumber;

    @FXML
    private PasswordField password2;

    @FXML
    private DatePicker birthady;

    @FXML
    private PasswordField password1;

    @FXML
    private RadioButton maleBotton;

    @FXML
    private RadioButton famaleBotton;

    @FXML
    private TextField userName;

    @FXML
    private Button closeButton;

    private String gender;
    @FXML
    private Button button;
    private Image image;

    @FXML
    private void getImage(MouseEvent event) {
        image = new Image(url.getText());
        imageview.setImage(image);
    }

    @FXML
    private void backtToLogin(MouseEvent event) {
        Stage stage = (Stage) closeButton.getScene().getWindow();
        stage.close();
    }

    @FXML
    private void register(MouseEvent event) throws IOException {
        User user = new User();
        user.setSex(gender);
        user.setAddress(area.getText());
        user.setName(userName.getText());
        user.setPhone(phoneNumber.getText());
        user.setBirthDay(birthady.getValue().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        user.setRegisterDate(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        if (password1.getText() != null && password2.getText() != null) {
            if (password1.getText().equals(password2.getText())) {
                user.setPassword(password1.getText());
            }
        }
        WritableImage image = imageview.snapshot(new SnapshotParameters(), null);
        try {
            ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", new File("photo/temp.png"));
        } catch (IOException ex) {
        }
        Connect connect=Connect.getConnect();
        connect.startConnect();
        String register = connect.register(user);
        try {
            ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", new File(String.format("photo/%s.png",register)));
        } catch (IOException ex) {
        }
        System.out.println("账号:"+register);
        connect.disconnect();
        
    }


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        final ToggleGroup group = new ToggleGroup();

        famaleBotton.setToggleGroup(group);
        famaleBotton.setUserData(1);

        maleBotton.setToggleGroup(group);
        maleBotton.setUserData(0);

        // 选中某个单选框时输出选中的值
        group.selectedToggleProperty().addListener(
                new ChangeListener<Toggle>() {
                    @Override
                    public void changed(ObservableValue<? extends Toggle> ov, Toggle oldToggle, Toggle newToggle) {
                        if (group.getSelectedToggle() != null) {
                            System.out.println(group.getSelectedToggle().getUserData());
                            gender = (Integer) group.getSelectedToggle().getUserData() == 0 ? "男" : "女";
                        }
                    }
                });
    }


    public void setMouse(DragEvent dragEvent) {
        dragEvent.acceptTransferModes(TransferMode.ANY);
    }

    public void loadImage(DragEvent dragEvent) {
        Dragboard dragboard = dragEvent.getDragboard();
        if (dragboard.hasFiles()) {
            String path = dragboard.getFiles().get(0).getAbsolutePath();
            path = "file:" + path;
            url.setText(path);
            this.getImage(null);
        }
    }
}
